﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

/// <summary>
/// 单词展现使用了之前用Django写过的记单词网站后端，直接使用webbrowser展示了所有单词
/// </summary>

namespace WindowsFormsApp1 {
    public partial class Form1 : Form {
        MySqlConnection conn;//数据库连接
        //textbox1的默认值
        private const String text = "请输入英语单词";
        public Form1() {
            InitializeComponent();
            webBrowser1.ScriptErrorsSuppressed = true;

            //为textbox1设置默认值和默认值的字体颜色
            textBox1.Text = text;
            textBox1.ForeColor = Color.Gray;
            //连接mysql
            String connetStr = "server=127.0.0.1;port=3306;user=root;password=123456; database=word_assistant_db;";
            //usr:用户名，password：数据库密码，database：数据库名
            conn = new MySqlConnection(connetStr);
        }

        //获取焦点事件 Enter
        private void textBox1_Enter(object sender, EventArgs e) {
            if (textBox1.Text == text) {
                textBox1.Text = "";
                textBox1.ForeColor = Color.Black;
            }
        }
        //失去焦点事件 Leave
        private void textBox1_Leave(object sender, EventArgs e) {
            if (String.IsNullOrEmpty(textBox1.Text)) {
                textBox1.Text = text;
                textBox1.ForeColor = Color.Gray;
            }
        }
        private void label2_Click(object sender, EventArgs e) {
            try {
                string id = comboBox1.Text;
                string value = textBox1.Text;
                conn.Open();//打开通道建立连接
                string sql = "select* from word_word where word_id=@word_id and word_value=@word_value";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("word_id", id);
                cmd.Parameters.AddWithValue("word_value", value);
                MySqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read()) label1.Text = "正确！";
                else label1.Text = "错误！";

            }
            catch(MySqlException) {
                label1.Text = "数据库查询出错！";
            }
            finally {
                conn.Close();
            }
        }
    }
}
